package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.BR
import com.stars.empoly.R
import com.stars.empoly.activity.LoginActivity
import com.stars.empoly.config.ConfigMMKV
import com.stars.empoly.utils.MUiSwitch
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.MMKVManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

class ForgetPasswordVModel(application: Application) : BViewModel<BRepository>(application) {

    var codeTip: MutableLiveData<String> = MutableLiveData(getCodeStr)
    private var disposable: Disposable? = null


    //类型
    var one= MutableLiveData<Boolean>(true)
    var adminType="main"//driver是司机 main是货主


    companion object {
        const val time = 60
        var timeStart: Long = 1
        const val getCodeStr = "获取验证码"

    }

    @Bindable
    var phone: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }

    @Bindable
    var code: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.code)
        }

    @Bindable
    var passWord: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWord)
        }

    @Bindable
    var passWordTo: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.passWordTo)
        }





    fun toChose(type: Int){

        if(type==1){
            one.value=true
            adminType="main"
        }else{
            one.value=false
            adminType="driver"
        }

    }


    fun toSubmit() {
        if (TextUtils.isEmpty(phone)) {
            toast("请输入手机号")
            return
        }

        if (TextUtils.isEmpty(code)) {
            toast("请输入验证码")
            return
        }
        if (TextUtils.isEmpty(passWord)) {
            toast("请输入密码")
            return
        }
        if(TextUtils.isEmpty(passWordTo)){
            toast("请输入确认密码")
            return
        }

        if(!passWord.equals(passWordTo)){
            toast("输入的密码不一致！")
            return
        }


        viewModelScope.launch {


            val it = repository().lreq { NetEngine.service.setPassword(adminType,phone,passWord, code) }
            if (it.ok(true)) {

                if(null!=repository().context) {
                    MUiSwitch.single(repository().context!!, LoginActivity::class.java)
                    var con = repository().context as Activity
                    con.finish()
                }
            }

        }


    }


    fun getSmsCode(start: Long = 1) {
        if (codeTip.value != getCodeStr) {
            return
        }

        if (phone.isNullOrEmpty()) {
            toast(R.string.please_input_user_phone)
            return
        }

        viewModelScope.launch {

            val it = repository().lreq { NetEngine.service.getPassword(phone,"resetpwd",adminType) }
            if (it.ok(true)) {
                startTimer(start)
            }

        }

    }

    private fun startTimer(start: Long = 1) {
        disposeTimer()
        disposable = Observable.intervalRange(start, time - start + 1, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext {
                    timeStart = it
                    codeTip.value = if (time - it > 0) "${time - it}秒后重发" else {
                        getCodeStr
                    }
                }
                .doOnComplete() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .doOnDispose() {
                    MMKVManager.instance().encode(ConfigMMKV.REGISTER_TIME_START, 60)
                }
                .subscribe()
    }

    private fun disposeTimer() {
        disposable?.isDisposed ?: disposable?.dispose()
    }

    override fun onCleared() {
        super.onCleared()
        disposeTimer()
    }


}



